using Microsoft.AspNetCore.Mvc;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Service.ProdManage.IProdManageService;
using ZR.Admin.WebApi.Filters;
using MiniExcelLibs;
using System.Data;


//创建时间：2025-04-28
namespace ZR.Admin.WebApi.Controllers.ProdManage
{
    /// <summary>
    /// 生产计划
    /// </summary>
    [Verify]
    [Route("ProdManage/ProdPlan")]
    public class ProdPlanController : BaseController
    {
        /// <summary>
        /// 生产计划接口
        /// </summary>
        private readonly IProdPlanService _ProdPlanService;

        public ProdPlanController(IProdPlanService ProdPlanService)
        {
            _ProdPlanService = ProdPlanService;
        }

        /// <summary>
        /// 查询生产计划列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("pageList")]
        [ActionPermissionFilter(Permission = "prod:plan:list")]
        public IActionResult PageProdPlan([FromQuery] ProdPlanQueryDto parm)
        {
            var response = _ProdPlanService.GetPageList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询生产计划详情
        /// </summary>
        /// <param name="PlanNo"></param>
        /// <returns></returns>
        [HttpGet("{PlanNo}")]
        [ActionPermissionFilter(Permission = "prod:plan:query")]
        public IActionResult GetProdPlan(string PlanNo)
        {
            var response = _ProdPlanService.GetInfo(PlanNo);

            var info = response.Adapt<ProdPlanDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加生产计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "prod:plan:add")]
        [Log(Title = "生产计划", BusinessType = BusinessType.INSERT)]
        public IActionResult AddProdPlan([FromBody] ProdPlanDto parm)
        {
            var modal = parm.Adapt<ProdPlan>().ToCreate(HttpContext);

            var response = _ProdPlanService.AddProdPlan(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新生产计划
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "prod:plan:edit")]
        [Log(Title = "生产计划", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateProdPlan([FromBody] ProdPlanDto parm)
        {
            var modal = parm.Adapt<ProdPlan>().ToUpdate(HttpContext);
            var response = _ProdPlanService.UpdateProdPlan(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除生产计划
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "prod:plan:delete")]
        [Log(Title = "生产计划", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteProdPlan([FromBody] string[] ids)
        {
            return ToResponse(_ProdPlanService.Delete(ids));
        }

        /// <summary>
        /// 导出生产计划
        /// </summary>
        /// <returns></returns>
        [Log(Title = "生产计划", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "prod:plan:export")]
        public IActionResult Export([FromQuery] ProdPlanQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _ProdPlanService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "生产计划", "生产计划");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "生产计划导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        [ActionPermissionFilter(Permission = "prod:plan:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<ProdPlanDto> list = new();
            using (var stream = formFile.OpenReadStream())
            {
                list = stream.Query<ProdPlanDto>(startCell: "A1").ToList();
            }

            return SUCCESS(_ProdPlanService.ImportProdPlan(list.Adapt<List<ProdPlan>>()));
        }

        /// <summary>
        /// 生产计划导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "生产计划模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel()
        {
            var result = DownloadImportTemplate(new List<ProdPlanDto>() { }, "ProdPlan");
            return ExportExcel(result.Item2, result.Item1);

            //(string, string) result = DownloadImportTemplate("KD-生产计划导入模板");
            //return ExportExcel(result.Item2, result.Item1);
        }

    }
}